package com.orange.wwwapi.controller.car;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.user.UserCar;
import com.orange.core.pojo.req.car.UserCarPageReq;
import com.orange.core.util.PrincipalUtil;
import com.orange.core.validator.group.Update;
import com.orange.wwwapi.pojo.rsp.user.UserCarParkFeeRsp;
import com.orange.wwwapi.service.carpark.KeyTopCarParkService;
import com.orange.wwwapi.service.carpark.YmiotCarParkService;
import com.orange.wwwapi.service.user.UserCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * 我的车辆
 */
@RestController
@RequestMapping("/userCar")
public class UserCarController {

    private final UserCarService userCarService;
    private final YmiotCarParkService ymiotCarParkService;
    private final KeyTopCarParkService keyTopCarParkService;

    public UserCarController(UserCarService userCarService,
                             @Autowired(required = false) YmiotCarParkService ymiotCarParkService,
                             @Autowired(required = false) KeyTopCarParkService keyTopCarParkService) {
        this.userCarService = userCarService;
        this.ymiotCarParkService = ymiotCarParkService;
        this.keyTopCarParkService = keyTopCarParkService;
    }

    /**
     * 分页
     */
    @PostMapping("/page")
    public R<Page<UserCar>> page(@RequestBody PageReqDto<UserCarPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new UserCarPageReq());
        }
        req.getCondition().setUserId(PrincipalUtil.getId());
        return R.ok(userCarService.page(req));
    }

    /**
     * 新增
     */
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated UserCar userCar) {
        return R.ok(userCarService.save(userCar.withId(null)));
    }

    /**
     * 编辑
     */
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated(Update.class) UserCar userCar) {
        return R.ok(userCarService.update(userCar));
    }

    /**
     * 解绑
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable int id) {
        return R.ok(userCarService.unbind(id));
    }

    /**
     * 查询停车缴费信息
     */
    @GetMapping("/carParkFee/{plateNo}")
    public R<UserCarParkFeeRsp> carParkFee(@PathVariable String plateNo) {
        UserCarParkFeeRsp carParkFee;
        if (ymiotCarParkService != null) {
            carParkFee = ymiotCarParkService.getCarParkFee(plateNo, Optional.ofNullable(PrincipalUtil.getId()).orElse(0));
        } else {
            carParkFee = keyTopCarParkService.getCarParkFee(plateNo, Optional.ofNullable(PrincipalUtil.getId()).orElse(0));
        }
        return R.ok(carParkFee);
    }

    /**
     * 兑换
     */
    @PostMapping("/exchangeCoupon/{plateNo}")
    public R<String> exchangeCoupon(@PathVariable String plateNo) {
        return R.ok(ymiotCarParkService.exchangeCoupon(plateNo, Optional.ofNullable(PrincipalUtil.getId()).orElse(0)));
    }

    /**
     * 支付停车费
     */
    @PostMapping("/payParkingFee/{plateNo}")
    public R<Integer> payParkingFee(@PathVariable String plateNo) {
        return R.ok(keyTopCarParkService.payParkingFee(plateNo, Optional.ofNullable(PrincipalUtil.getId()).orElse(0)));
    }
}
